﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using Meteorite;
using System.Diagnostics;

namespace Meteorite
{
    public class ControlPad : CCSprite, ICCTargetedTouchDelegate
    {
        CCSprite Sprbutton;
        

        CCPoint TouchPt = CCPoint.Zero;
        CCPoint CenterPt = CCPoint.Zero;
        CCPoint Offset = CCPoint.Zero;
        float Radius = 0;
        float SquareRadius = 0;
        bool Pressed;
        
      

        public ControlPad()
        {
            base.initWithFile(GameConstant.ControlPannelBkg);

            Sprbutton = CCSprite.spriteWithFile(GameConstant.ControlPannelBtn);
            
            addChild(Sprbutton);

            Pressed = false;

            Radius = Texture.getContentSize().width / 2;
            CenterPt.x = position.x + Radius;
            CenterPt.y = position.y + Radius;
            SquareRadius = Radius * Radius;
        }
        public override void onEnter()
        {
            CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, 0, true);
            base.onEnter();
        }

        public override void onExit()
        {
            CCTouchDispatcher.sharedDispatcher().removeDelegate(this);
            base.onExit();
        }

        bool IsPointInPad(CCPoint Pt)
        {
            float DisSquare = (Pt.x - CenterPt.x) * (Pt.x - CenterPt.x) + (Pt.y - CenterPt.y) * (Pt.y - CenterPt.y);
            return DisSquare <= SquareRadius;
        }
        public void Loop()
        {
            Offset = CCPoint.Zero;

            if (Pressed && IsPointInPad(TouchPt))
            {
                Offset.x = TouchPt.x - CenterPt.x;
                Offset.y = TouchPt.y - CenterPt.y;
            }
            else
            {
                //Debug.WriteLine("-----{0}{1}", Offset.x, Offset.y);
            }
            Sprbutton.position = new CCPoint(Radius + Offset.x, Radius + Offset.y);
        }

        public virtual bool ccTouchBegan(CCTouch touch, CCEvent eventer)
        {
            CCPoint touchPoint = touch.locationInView(touch.view());
            TouchPt = CCDirector.sharedDirector().convertToGL(touchPoint);
            Pressed = true;
            
            return true;
        }

        public virtual void ccTouchMoved(CCTouch touch, CCEvent eventer)
        {
            CCPoint touchPoint = touch.locationInView(touch.view());
            TouchPt = CCDirector.sharedDirector().convertToGL(touchPoint);
            Pressed = true;
        }

        public virtual void ccTouchEnded(CCTouch touch, CCEvent eventer)
        {
            Pressed = false;
            
        }
        public void ccTouchCancelled(CCTouch pTouch, CCEvent pEvent)
        {
            Pressed = false;
        }
        public CCPoint GetOffsetPercent()
        {
            return new CCPoint(Offset.x / Radius, Offset.y / Radius);
        }
    }
}
